<?php

namespace app\index\controller;


use think\Controller;

class GetIp extends Controller
{
	/*
	* 成功获取到用户访问的IP地址
	*/
	public function getIp()
	{
		$ip=false;
		if(!empty($_SERVER["HTTP_CLIENT_IP"]))
		{
			$ip = $_SERVER["HTTP_CLIENT_IP"];
		}
		if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
			$ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
			if ($ip) { array_unshift($ips, $ip); $ip = FALSE; }
			for ($i = 0; $i < count($ips); $i++) {
				if (!eregi ("^(10│172.16│192.168).", $ips[$i])) {
					$ip = $ips[$i];
					break;
				}
			}
		}
		//因为是本地访问,所以返回127.0.0.1
		return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
	}

	/*
	* 成功获取到用户的访问地址
	*/
	public function getArea()
	{	 
		 $ip = $this->getIp();
   		 $res1 = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=$ip");
   		  //dp($res1);
    	 $res1 = json_decode($res1,true);
 
		
   		if ($res1[ "code"]==0)
   		{
        	return $res1['data']["country"].$res1['data'][ "region"].$res1['data']["city"]."_".$res1['data'][ "isp"];
    	}else{
        	return "未知";
    	} 
	}

	/*
	* 成功获取
	*/
	public function getip2()
	{
		$ip = false;
		if(!empty( $_SERVER ["HTTP_CLIENT_IP"]))
		{
			$ip = $_SERVER ["HTTP_CLIENT_IP"];
		}
		if (!empty( $_SERVER ['HTTP_X_FORWARDED_FOR']))
		{
			$ips = explode ( ", ",$_SERVER ['HTTP_X_FORWARDED_FOR']);
			if ($ip)
			{
				array_unshift ($ips,$ip);
				$ip = FALSE;
			}
			for($i = 0; $i < count ( $ips ); $i ++)
			{
				if (!eregi( "^(10|172\.16|192\.168)\.",$ips[$i]))
				{
					$ip =$ips[$i];
					break;
				}
			}
		}
		$ip_str=($ip?$ip:$_SERVER['REMOTE_ADDR']);
		$ip_arr=explode(',',$ip_str);
		return $ip_arr[0];
	}


}